package com.example.xuitest;

import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xuitest.fragment.IndexFragment;
import com.example.xuitest.fragment.MyInfoFragment;
import com.example.xuitest.fragment.ToolsFragment;
import com.hjm.bottomtabbar.BottomTabBar;
import com.xuexiang.xrouter.launcher.XRouter;
import com.xuexiang.xui.XUI;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    // 用来计算返回键的点击间隔时间
    private long exitTime = 0;
    int overPosition;
    private float mPosX, mPosY, mCurPosX, mCurPosY;

   /* @BindView(R.id.title_index_bar)
    TitleBar titleIndexBar;*/
    @BindView(R.id.bottom_tab_bar)
    BottomTabBar bottomTabBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initXUI();
        XRouter.init(this.getApplication());
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
      //  titleIndexBar.disableLeftView();
        setGestureListener();
        bottomTabBar.init(getSupportFragmentManager())
                .setImgSize(25,25)
                .setFontSize(12)
                .setTabPadding(4,6,10)
                .setChangeColor(Color.RED,Color.DKGRAY)
                .addTabItem("主页",R.drawable.tab_contact_selector, IndexFragment.class)
                .addTabItem("工具",R.drawable.tab_sign_selector, ToolsFragment.class)
                .addTabItem("我的",R.drawable.icon_head_default, MyInfoFragment.class)
                .isShowDivider(false)
                .setOnTabChangeListener((position, name, view) -> {
                  //  titleIndexBar.setTitle(name);
                    overPosition = position;
                });



    }

    private void setGestureListener() {
        bottomTabBar.setOnTouchListener((v, event) -> {
            // TODO Auto-generated method stub
            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    mPosX = event.getX();
                    mPosY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mCurPosX = event.getX();
                    mCurPosY = event.getY();

                    break;
                case MotionEvent.ACTION_UP:
//                        if (mCurPosY - mPosY > 0
//                                && (Math.abs(mCurPosY - mPosY) > 25)) {
//                            //向下滑動
//                        } else if (mCurPosY - mPosY < 0
//                                && (Math.abs(mCurPosY - mPosY) > 25)) {
//                            //向上滑动
//                        }
                    if (mCurPosX - mPosX > 0
                            && (Math.abs(mCurPosX - mPosX) > 25)) {
                        //向左滑動
                        bottomTabBar.setCurrentTab(overPosition - 1);

                    } else if (mCurPosX - mPosX < 0
                            && (Math.abs(mCurPosX - mPosX) > 25)) {
                        //向右滑动
                        bottomTabBar.setCurrentTab(overPosition + 1);
                    }
                    break;
            }
            return true;
        });
    }


    private void initXUI() {
        XUI.init(this.getApplication());
        XUI.debug(true);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                //弹出提示，可以有多种方式
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


}
